package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduResourceMapper;
import com.zenith.edu.po.domain.EduResource;
import com.zenith.edu.po.dto.EduResourceDTO;
import com.zenith.edu.po.vo.EduResourceVO;
import com.zenith.edu.service.EduResourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduResourceServiceImpl extends ServiceImpl<EduResourceMapper, EduResource> implements EduResourceService {
    
    @Override
    public EduResourceVO createResource(EduResourceDTO dto) {
        EduResource resource = new EduResource();
        BeanUtils.copyProperties(dto, resource);
        this.save(resource);
        
        EduResourceVO vo = new EduResourceVO();
        BeanUtils.copyProperties(resource, vo);
        return vo;
    }
    
    @Override
    public EduResourceVO getResourceById(Long id) {
        EduResource resource = this.getById(id);
        if (resource == null) {
            return null;
        }
        
        EduResourceVO vo = new EduResourceVO();
        BeanUtils.copyProperties(resource, vo);
        return vo;
    }
    
    @Override
    public EduResourceVO updateResource(EduResourceDTO dto) {
        EduResource resource = new EduResource();
        BeanUtils.copyProperties(dto, resource);
        this.updateById(resource);
        
        EduResourceVO vo = new EduResourceVO();
        BeanUtils.copyProperties(resource, vo);
        return vo;
    }
    
    @Override
    public boolean deleteResource(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduResourceVO> getResourceList() {
        List<EduResource> resourceList = this.list();
        return resourceList.stream().map(resource -> {
            EduResourceVO vo = new EduResourceVO();
            BeanUtils.copyProperties(resource, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}